/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
   Bool optionss;
   GuiObjs OtionsButtonG;
static ListColumn mode_list_column[]=
{
   ListColumn(DATA_VECI2, 0, SIZE(VecI2), LCW_PARENT, L"Size"),
};
static CChar8 *tabs_t[]=
{
   "Video",
   "Audio & Misc",
};
static CChar8 *off_on[]=
{
   "Off",
   "On" ,
};
static CChar8 *renderer[]=
{
   "Deferred",
   "Forward" ,
   "Simple"  ,
};
static CChar8 *super_sample[]=
{
   "0",
   "1",
   "2",
   "3",
   "4",
   "5",
};
static CChar8 *edge[]=
{
   "Off" ,
   "Thin",
   "Fat" ,
   "FXAA",
   "MLAA",
};
static CChar8 *shadow_size[]=
{
   "512",
   "1024",
   "1536",
   "2048",
};
static CChar8 *shadow_number[]=
{
   "1",
   "2",
   "3",
   "4",
   "5",
   "6",
};
static CChar8 *shadow_soft[]=
{
   "0",
   "1",
   "2",
};
static CChar8 *bump[]=
{
   "Flat",
   "Normal",
   "Parallax",
   "Relief",
};
static CChar8 *motion[]=
{
   "Off",
   "Medium",
   "High",
};
static CChar8 *motion_leaks[]=
{
   "Off",
   "Low",
   "Medium",
   "High",
};
static CChar8 *ambient[]=
{
   "Off",
   "Low",
   "Medium",
   "High",
   "Ultra",
};
/******************************************************************************/

static Flt FracToGamma(Flt frac ) {return frac -0.5;}
static Flt GammaToFrac(Flt gamma) {return gamma+0.5;}

static Flt FracToMouse(Flt frac ) {return ScaleFactor (frac-0.5f);}
static Flt MouseToFrac(Flt mouse) {return ScaleFactorR(mouse)+0.5f;}

static Flt FracToGrass(Flt frac ) {return Lerp (0.0f, 100.0f, frac );}
static Flt GrassToFrac(Flt grass) {return LerpR(0.0f, 100.0f, grass);}

static Int BumpModesSupported()
{
   switch(Renderer.type())
   {
      case RT_DEFERRED: return 4;
      case RT_FORWARD : return 2;
      default         : return 0;
   }
}
/******************************************************************************/
STRUCT(OptionsClass , GuiObjs)
//{
   Button tabs,etc;
   Region Musicc, Videoo;
   STRUCT(Window , ::Window)
   //{

      virtual void update(GuiPC &gpc)
      {
		  
         if(Gui.window()==this && !Gui.cmenu())
         {
            if(Kb.bp(KB_ESC) || Kb.bp(KB_ENTER) || Ms.bp(1))
            {
               Kb.eat();
               Ms.eat();
               fadeOut();
            }
            REPA(Touches)if(Touches[i].guiObj()==this && Touches[i].db())
            {
               fadeOut();
            }
         }

         super::update(gpc);
      }
   };

   static void Mode        (OptionsClass &o) {                      VecI2 mode=TextVecI2(o.getComboBox("mode"         ).text()); D.mode(mode.x, mode.y);}
   static void Full        (OptionsClass &o) {  D       .full          (                 o.getComboBox("full"         )()!=0);}
   static void Sync        (OptionsClass &o) {  D       .sync          (                 o.getComboBox("sync"         )()!=0);}
   static void Renderer    (OptionsClass &o) {::Renderer.type          (RENDER_TYPE     (o.getComboBox("renderer"     )())  ); o.setVisibility(); o.toGui();}
   static void SuperSample (OptionsClass &o) {  D       .superSample   (                 o.getComboBox("super"        )()   );}
   static void EdgeSoft    (OptionsClass &o) {  D       .edgeSoftenMode(EDGE_SOFTEN_MODE(o.getComboBox("edge"         )())  );}
   static void Shadow      (OptionsClass &o) {  D       .shadowMode    (                 o.getComboBox("shadow"       )() ? SHADOW_MAP : SHADOW_NONE); o.setVisibility();}
   static void ShadowSize  (OptionsClass &o) {  D       .shadowMapSize (         TextInt(o.getComboBox("shadow size"  ).text()));}
   static void ShadowNumber(OptionsClass &o) {  D       .shadowMapNum  (                 o.getComboBox("shadow number")()+1 );}
   static void ShadowSoft  (OptionsClass &o) {  D       .shadowSoft    (                 o.getComboBox("shadow soft"  )()   );}
   static void ShadowJitter(OptionsClass &o) {  D       .shadowJitter  (                 o.getComboBox("shadow jitter")()!=0);}
   static void Bump        (OptionsClass &o) {  D       .bumpMode      (BUMP_MODE       (o.getComboBox("bump"         )())  );}
   static void MotionBlur  (OptionsClass &o) {                               Byte motion=o.getComboBox("motion"       )()    ; D.motionMode(motion ? MOTION_HIGH : MOTION_NONE).motionSmooth(motion==2); o.setVisibility();}
   static void MotionLeaks (OptionsClass &o) {  D       .motionLeaks   (                 o.getComboBox("motion leaks" )()   );}
   static void AO          (OptionsClass &o) {  D       .ambientMode   (AMBIENT_MODE    (o.getComboBox("ao"           )())  );}
   static void Hdr         (OptionsClass &o) {  D       .hdr           (                 o.getComboBox("hdr"          )()!=0);}
   static void Hprt        (OptionsClass &o) {  D       .hpRt          (                 o.getComboBox("hprt"         )()!=0);}

   static void Global (OptionsClass &o) {SoundVolume.global (o.getSlider("global" )());}
   static void Sounds (OptionsClass &o) {SoundVolume.fx     (o.getSlider("sounds" )());}
   static void Music  (OptionsClass &o) {SoundVolume.music  (o.getSlider("music"  )());}
   static void Ambient(OptionsClass &o) {SoundVolume.ambient(o.getSlider("ambient")());}

   static void Gamma(OptionsClass &o) {D .gamma     (FracToGamma   (o.getSlider  ("gamma")()));}
   static void Mouse(OptionsClass &o) {Ms.speed     (FracToMouse   (o.getSlider  ("mouse")()));}
   static void Grass(OptionsClass &o) {D .grassRange(FracToGrass   (o.getSlider  ("grass")()));}
   static void MusicEtcc(Ptr){Options.MusicEtc();}
   static void VideoOptions(Ptr){Options.VideoOptionsPt();}
   void create()
   {
      replaceWindow<Window>(); load("gui/options.gobj"); hide(); Gui+=T;


   #ifdef MOBILE
      getRegion("tabs" ).hide();
      getRegion("video").hide();
      getText  ("gamma").hide(); getSlider("gamma").hide();
      getText  ("mouse").hide(); getSlider("mouse").hide();
   #else

	  tabs+=getButton("video").func(VideoOptions);
      etc+=getButton("Music").func(MusicEtcc);
	  Musicc+=getRegion("Music").hide();
	  Videoo+=getRegion("video").show();
   #endif

      getComboBox("mode"         ).setColumns(mode_list_column, Elms(mode_list_column)).setData(D.modeSize(), D.modes()).func(Mode, T);
      getComboBox("full"         ).setData(off_on       , Elms(off_on       )).func(Full        , T);
      getComboBox("sync"         ).setData(off_on       , Elms(off_on       )).func(Sync        , T);
      getComboBox("renderer"     ).setData(renderer     , Elms(renderer     )).func(Renderer    , T);
      getComboBox("super"        ).setData(super_sample , Elms(super_sample )).func(SuperSample , T);
      getComboBox("edge"         ).setData(edge         , Elms(edge         )).func(EdgeSoft    , T);
      getComboBox("shadow"       ).setData(off_on       , Elms(off_on       )).func(Shadow      , T);
      getComboBox("shadow size"  ).setData(shadow_size  , Elms(shadow_size  )).func(ShadowSize  , T);
      getComboBox("shadow number").setData(shadow_number, Elms(shadow_number)).func(ShadowNumber, T);
      getComboBox("shadow soft"  ).setData(shadow_soft  , Elms(shadow_soft  )).func(ShadowSoft  , T);
      getComboBox("shadow jitter").setData(off_on       , Elms(off_on       )).func(ShadowJitter, T);
      getComboBox("bump"         ).setData(bump         , Elms(bump         )).func(Bump        , T);
      getComboBox("motion"       ).setData(motion       , Elms(motion       )).func(MotionBlur  , T);
      getComboBox("motion leaks" ).setData(motion_leaks , Elms(motion_leaks )).func(MotionLeaks , T);
      getComboBox("ao"           ).setData(ambient      , Elms(ambient      )).func(AO          , T);
      getComboBox("hdr"          ).setData(off_on       , Elms(off_on       )).func(Hdr         , T);
      getComboBox("hprt"         ).setData(off_on       , Elms(off_on       )).func(Hprt        , T);

      getSlider("global" ).func(Global , T);
      getSlider("sounds" ).func(Sounds , T);
      getSlider("music"  ).func(Music  , T);
      getSlider("ambient").func(Ambient, T);

      getSlider  ("gamma").func(Gamma, T);
      getSlider  ("mouse").func(Mouse, T);
      getSlider  ("grass").func(Grass, T);

      toGui();
   }
   
   void toGui()
   {
      getComboBox("mode"         ).setText(  S+VecI2(D.x(), D.y())                        , true, true);
      getComboBox("full"         ).set    (    D.full          ()                         , true);
      getComboBox("sync"         ).set    (    D.sync          ()                         , true);
      getComboBox("renderer"     ).set    (  ::Renderer.type   ()                         , true);
      getComboBox("super"        ).set    (    D.superSample   ()                         , true);
      getComboBox("edge"         ).set    (    D.edgeSoftenMode()                         , true);
      getComboBox("shadow"       ).set    (    D.shadowMode    ()!=SHADOW_NONE            , true);
      getComboBox("shadow size"  ).setText(  S+D.shadowMapSize ()                         , true, true);
      getComboBox("shadow number").set    (    D.shadowMapNum  ()-1                       , true);
      getComboBox("shadow soft"  ).set    (    D.shadowSoft    ()                         , true);
      getComboBox("shadow jitter").set    (    D.shadowJitter  ()                         , true);
      getComboBox("bump"         ).set    (Min(D.bumpMode      (), BumpModesSupported()-1), true);
      getComboBox("motion"       ).set    (    D.motionMode() ? D.motionSmooth()+1 : 0    , true);
      getComboBox("motion leaks" ).set    (    D.motionLeaks   ()                         , true);
      getComboBox("ao"           ).set    (    D.ambientMode   ()                         , true);
      getComboBox("hdr"          ).set    (    D.hdr           ()                         , true);
      getComboBox("hprt"         ).set    (    D.hpRt          ()                         , true);
      
      getSlider("global" ).set(SoundVolume.global (), true);
      getSlider("sounds" ).set(SoundVolume.fx     (), true);
      getSlider("music"  ).set(SoundVolume.music  (), true);
      getSlider("ambient").set(SoundVolume.ambient(), true);

      getSlider  ("gamma").set(GammaToFrac   (D .gamma     ()), true);
      getSlider  ("mouse").set(MouseToFrac   (Ms.speed     ()), true);
      getSlider  ("grass").set(GrassToFrac   (D .grassRange()), true);

      setVisibility();
   }

   void setVisibility()
   {
      getComboBox("bump"         ).setData(bump, Min(Elms(bump), BumpModesSupported()));

      getComboBox("shadow"       ).visible(::Renderer.type()!=RT_SIMPLE  );
      getComboBox("shadow size"  ).visible(::Renderer.type()!=RT_SIMPLE   && D.shadowMode()==SHADOW_MAP );
      getComboBox("shadow number").visible(::Renderer.type()!=RT_SIMPLE   && D.shadowMode()==SHADOW_MAP );
      getComboBox("shadow soft"  ).visible(::Renderer.type()==RT_DEFERRED && D.shadowMode()!=SHADOW_NONE);
      getComboBox("shadow jitter").visible(::Renderer.type()!=RT_SIMPLE   && D.shadowMode()==SHADOW_MAP );
      getComboBox("bump"         ).visible(::Renderer.type()!=RT_SIMPLE  );
      getComboBox("motion"       ).visible(::Renderer.type()==RT_DEFERRED);
      getComboBox("motion leaks" ).visible(::Renderer.type()==RT_DEFERRED && D.motionMode()!=MOTION_NONE);
      getComboBox("ao"           ).visible(::Renderer.type()==RT_DEFERRED);
   }

   Bool visible()
   {
      if(::Window *window=findWindow(S))return !window->hiding();
      return false;
   }
   
   void toggleVisibility()
   {
      if(::Window *window=findWindow(S))
      {
         window->fadeToggle();
      }
   }
   void MusicEtc()
   {
	   getRegion("Music").show();
	   getRegion("video").hide();
   }
   void VideoOptionsPt()
   {
	   getRegion("video").show();
	   getRegion("Music").hide();
   }
}Options;
/******************************************************************************/
Bool OptionsVisible() {return Options.visible();}
void OptionsCreate () {       Options.create ();}
void OptionsToggle () {       Options.toggleVisibility();}
void OptionsHide   () {       Options.hide   ();}
void OptionsToGui  () {       Options.toGui  ();}
/******************************************************************************/
